package com.jt.provider.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

@Component
public class TimeInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==preHandle==");
        //1.获取当前时间
        LocalTime now = LocalTime.now();//LocalTime为jdk8中的一个获取当前时间的api
        //2.获取当前的小时并进行逻辑判断
        int hour = now.getHour();
        System.out.println("hour:" + hour);
        if (hour < 9 || hour > 18) {
            throw new RuntimeException("请在9~18点范围内访问"); //return false
        }
        return true;//false请求到此结束，true表示放行，会去执行后续的拦截器或controller对象
    }
}

